package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Employee;
import com.model.Orders;
import com.service.EmpService;
import com.util.JdbcUtil;

/**
 * Servlet implementation class OrdersServlet
 */
@WebServlet("/ods")
public class OrdersServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		JdbcUtil jdbc = new JdbcUtil();
		String sql ="select * from orders order by oid";
		List<Orders> odss = jdbc.queryPreparedStatement(sql, Orders.class);
			
		request.setAttribute("odss",odss);
		request.getRequestDispatcher("AssignOrder.jsp").forward(request, response);
		
		
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		String oid = request.getParameter("oid");
		JdbcUtil jdbc = new JdbcUtil();
		String sql="delete from orders where oid=?";
		try {
			jdbc.updatePreparedStatement(sql,oid);
			request.getRequestDispatcher("ods?action=query").forward(request, response);
		
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("mess", "删除失败，请稍后再试");
		}
	}
	
	protected void toalert(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		String eid = request.getParameter("eid");
		String oid = request.getParameter("oid");
		request.setAttribute("eid", eid);
		request.setAttribute("oid", oid);
		request.getRequestDispatcher("order_edit.jsp").forward(request, response);
	}
	protected void alert(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		
		String eid = request.getParameter("eid");
		String oid = request.getParameter("oid");
		System.out.println(eid);
		System.out.println(oid);
		JdbcUtil jdbc = new JdbcUtil();
		StringBuffer sql =new StringBuffer();
		sql.append("update orders set eid = ");
		sql.append(eid);
		sql.append(" where oid='");
		sql.append(oid+"'");
		try {
			jdbc.updatePreparedStatement(sql.toString());
			System.out.println(sql.toString());
			
			//cxx   修改orderittem里面的eid
			String s = "update orderitem set eid = ? where oid = ?";
			jdbc.updatePreparedStatement(s,eid,oid);
			
			System.out.println("分配成功，我要跳转了");
			query(request, response);
		
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("mess", "分配失败，请稍后再试");
		}
	}
	

	
	
}
